/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-12-15
*/
package com.rzico.scenic.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.model.MapPoint;
import com.rzico.basics.model.ShopVo;
import com.rzico.scenic.entity.ScenicSpot;
import com.rzico.scenic.model.ScenicSpotVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.scenic.mapper.ScenicSpotMapper;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 旅游景区业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class ScenicSpotService extends BaseServiceImpl<ScenicSpot, String> {

    @Autowired
    private ScenicSpotMapper scenicSpotMapper;

    @Override
    public BaseMapper<ScenicSpot, String> getMapper() {
        return scenicSpotMapper;
    }

    /**
     * 添加喜欢人数(收藏人数)
     * @param params
     * @return
     */
    public Integer addLove(Map<String, Object> params){
        return scenicSpotMapper.addLove(params);
    }

    /**
     * 删除喜欢人数(收藏人数)
     * @param params
     * @return
     */
    public Integer decLvoe(Map<String, Object> params){
        return scenicSpotMapper.decLvoe(params);
    }

    /**
     * 首页查询景区
     * @param params
     * @param lat
     * @param lng
     * @return
     */
    public List<ScenicSpotVo> selectLbsList(Map<String, Object> params, Double lat, Double lng) {
        List<ScenicSpotVo> scenicSpotList = scenicSpotMapper.selectLbsList(params);
        if (lat!=null && lng !=null && !params.containsKey("sortField")) {
            for (ScenicSpotVo scenicSpotVo : scenicSpotList) {
                if (scenicSpotVo.getLat() != null && scenicSpotVo.getLng() != null) {
                    MapPoint point = new MapPoint();
                    point.setLng(lng);
                    point.setLat(lat);

                    MapPoint scenicSpotPoint = new MapPoint();
                    scenicSpotPoint.setLat(scenicSpotVo.getLat());
                    scenicSpotPoint.setLng(scenicSpotVo.getLng());

                    Double d = point.distatce(scenicSpotPoint);
                    scenicSpotVo.setDistance(d);
                }
            }
            //开始排序
            Collections.sort(scenicSpotList, (n1, n2) -> {
                if (n1.getDistance()==null) {
                        return -1;
                    }
                    if (n2.getDistance()==null) {
                        return 1;
                    }
                    return n1.getDistance().compareTo(n2.getDistance());
            });
        } else if(lat!=null && lng !=null && params.containsKey("sortField")){
            for (ScenicSpotVo scenicSpotVo : scenicSpotList) {
                if (scenicSpotVo.getLat() != null && scenicSpotVo.getLng() != null) {
                    MapPoint point = new MapPoint();
                    point.setLng(lng);
                    point.setLat(lat);

                    MapPoint scenicSpotPoint = new MapPoint();
                    scenicSpotPoint.setLat(scenicSpotVo.getLat());
                    scenicSpotPoint.setLng(scenicSpotVo.getLng());

                    Double d = point.distatce(scenicSpotPoint);
                    scenicSpotVo.setDistance(d);
                }
            }
        }

        return scenicSpotList;
    }
}
